﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.WorkTaskGrade.BLL;
using Yipusoft.WorkTaskGrade.Model;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class Task_WorkTask_TaskItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();

        }
    }

    void BindData()
    {
        Guid id = new Guid(Request["Id"]);
        WorkTaskGrade grade = new WorkTaskGrade();
        WorkTaskGradeInfo info = grade.GetDetail(id);
        string date = info.TaskDate.ToString("yyyy年MM月");
        DepartmentInfo department = Department.GetDepartment(info.DepartmentID);
        UserGrid.Caption = "<span>" + date + department.ShortName.Substring(5, department.ShortName.Length - 5) + info.WorkTask + "</span>";
        List<WorkTaskItemInfo> list = WorkTaskItem.GetList(" taskId='" + id.ToString() + "' order by OrderNo,CreateDate ");
        UserGrid.DataSource = list;
        UserGrid.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //保存
        Guid id = new Guid(Request["Id"]);

        WorkTaskItemInfo info = new WorkTaskItemInfo();
        info.Circs = SmartWeb.BLL.UserFun.TextToHtml(txtCirce.Text);
        info.ItemContent = SmartWeb.BLL.UserFun.TextToHtml(txtContent.Text);
        info.CreateDate = DateTime.Now;
        info.CreateUser = User.Identity.Name;
        info.ItemId = Guid.NewGuid();
        info.TaskId = id;
        info.OrderNo = Convert.ToInt32(ddlOrderNo.SelectedValue);
        info.FinishData = txtFinishDate.Text;
        id = WorkTaskItem.Insert(info);

        if (id != null)
        {
            Prompt.Text = "提示：添加成功！";
            BindData();
        }
        else
        {
            Prompt.Text = "提示：添加失败！";
        }
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");


            DropDownList ddlOrderNo = e.Row.FindControl("ddlOrderNo") as DropDownList;
            if (ddlOrderNo != null)
            {

                WorkTaskItemInfo item = e.Row.DataItem as WorkTaskItemInfo;

                ddlOrderNo.SelectedValue = item.OrderNo.ToString();


                TextBox ItemContent = e.Row.FindControl("txtItemContent") as TextBox;

                ItemContent.Text = SmartWeb.BLL.UserFun.HtmlToText(item.ItemContent);

                TextBox txtCircs1 = e.Row.FindControl("txtCircs") as TextBox;
                txtCircs1.Text = SmartWeb.BLL.UserFun.HtmlToText(item.Circs);

                TextBox txtFinishDate = e.Row.FindControl("txtFinishDate") as TextBox;
                txtFinishDate.Text = SmartWeb.BLL.UserFun.HtmlToText(item.FinishData);
            }


        }

    }
    protected void UserGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        UserGrid.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void UserGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

        UserGrid.EditIndex = -1;
        BindData();
    }
    protected void UserGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = UserGrid.Rows[e.RowIndex];

        Label lblId = row.FindControl("lblId") as Label;
        TextBox txtItemContent = row.FindControl("txtItemContent") as TextBox;
        TextBox txtCircs = row.FindControl("txtCircs") as TextBox;
        DropDownList ddlNo = row.FindControl("ddlOrderNo") as DropDownList;
        int OrderNo = Convert.ToInt32(ddlNo.SelectedValue);
        TextBox txtFinishDate = row.FindControl("txtFinishDate") as TextBox;

        WorkTaskItemInfo info = WorkTaskItem.GetDetail(new Guid(lblId.Text));
        info.Circs = SmartWeb.BLL.UserFun.TextToHtml(txtCircs.Text);
        info.ItemContent = SmartWeb.BLL.UserFun.TextToHtml(txtItemContent.Text);
        info.OrderNo = OrderNo;
        info.FinishData = txtFinishDate.Text;
        WorkTaskItem.Update(info);
        UserGrid.EditIndex = -1;
        BindData();
    }
    protected void UserGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            Guid id = new Guid(e.CommandArgument.ToString());
            if (WorkTaskItem.Delete(id))
            {

                Prompt.Text = "提示：删除成功！";
            }
            else
            {
                Prompt.Text = "提示：删除失败！";
            }
            BindData();
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("WorkTaskList.aspx");
    }
}
